home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / bashsrc.zoo / parse.y < prev    next >
Encoding:
Text File  |  1991-05-29  |  45.4 KB  |  1,910 lines

  1. /* Yacc grammar for bash. */
  2.  
  3. /*  Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file LICENSE.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. %{
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include "shell.h"
  25. #include "flags.h"
  26.  
  27. #ifdef READLINE
  28. #include <readline/readline.h>
  29. #include <readline/history.h>
  30. #endif
  31.  
  32. #define YYDEBUG 1
  33. extern int eof_encountered;
  34. extern int no_line_editing;
  35. extern int interactive;
  36. %}
  37.  
  38. %union {
  39.   WORD_DESC *word;        /* the word that we read. */
  40.   int number;            /* the number that we read. */
  41.   WORD_LIST *word_list;
  42.   COMMAND *command;
  43.   REDIRECT *redirect;
  44.   ELEMENT element;
  45.   PATTERN_LIST *pattern;
  46. }
  47.  
  48. /* Reserved words.  Members of the first group are only recognized
  49.    in the case that they are preceded by a list_terminator.  Members
  50.    of the second group are recognized only under special circumstances. */
  51. %token IF THEN ELSE ELIF FI CASE ESAC FOR WHILE UNTIL DO DONE FUNCTION
  52. %token IN
  53.  
  54. /* More general tokens. yylex () knows how to make these. */
  55. %token <word> WORD
  56. %token <number> NUMBER
  57. %token AND_AND OR_OR GREATER_GREATER LESS_LESS LESS_AND
  58. %token GREATER_AND SEMI_SEMI LESS_LESS_MINUS AND_GREATER
  59. %token DOUBLE_OPEN DOUBLE_CLOSE
  60.  
  61. /* The types that the various syntactical units return. */
  62.  
  63. %type <command> inputunit command list list0 list1 simple_list simple_list1 simple_command shell_command group_command
  64. %type <command> elif_clause
  65. %type <redirect> redirection redirections
  66. %type <element> simple_command_element
  67. %type <word_list> words pattern 
  68. %type <pattern> pattern_list case_clause_sequence case_clause_1 pattern_list_1
  69.  
  70. %start inputunit
  71.  
  72. %left '&' ';' '\n' yacc_EOF
  73. %left AND_AND OR_OR
  74. %left '|'
  75. %%
  76.  
  77. inputunit:    simple_list '\n'
  78.             {
  79.               /* Case of regular command.  Discard the error
  80.                  safety net,and return the command just parsed. */
  81.               global_command = $1;
  82.               eof_encountered = 0;
  83.               discard_parser_constructs (0);
  84.               YYACCEPT;
  85.             }
  86.     |    '\n'
  87.             {
  88.               /* Case of regular command, but not a very
  89.                  interesting one.  Return a NULL command. */
  90.               global_command = (COMMAND *)NULL;
  91.               YYACCEPT;
  92.             }
  93.     |
  94.         error '\n'
  95.             {
  96.               /* Error during parsing.  Return NULL command. */
  97.               global_command = (COMMAND *)NULL;
  98.               eof_encountered = 0;
  99.               discard_parser_constructs (1);
  100.               if (interactive)
  101.                 YYACCEPT
  102.               else
  103.                 YYABORT;
  104.             }
  105.     |
  106.         yacc_EOF
  107.             {
  108.               /* Case of EOF seen by itself.  Do ignoreeof or 
  109.                  not. */
  110.               global_command = (COMMAND *)NULL;
  111.               handle_eof_input_unit ();
  112.               YYACCEPT;
  113.             }
  114.     ;
  115.  
  116. words:    
  117.             { $$ = NULL; }
  118.     |    words WORD
  119.             { $$ = make_word_list ($2, $1); }
  120.     ;
  121.  
  122. redirection:    '>' WORD
  123.             { $$ = make_redirection ( 1, r_output_direction, $2); }
  124.     |    '<' WORD
  125.             { $$ = make_redirection ( 0, r_input_direction, $2); }
  126.     |    NUMBER '>' WORD
  127.             { $$ = make_redirection ($1, r_output_direction, $3); }
  128.     |    NUMBER '<' WORD
  129.             { $$ = make_redirection ($1, r_input_direction, $3); }
  130.     |    GREATER_GREATER WORD
  131.             { $$ = make_redirection ( 1, r_appending_to, $2); }
  132.     |    NUMBER GREATER_GREATER WORD
  133.             { $$ = make_redirection ($1, r_appending_to, $3); }
  134.     |    LESS_LESS WORD
  135.             { $$ = make_redirection ( 0, r_reading_until, $2); }
  136.     |    NUMBER LESS_LESS WORD
  137.             { $$ = make_redirection ($1, r_reading_until, $3); }
  138.     |    LESS_AND NUMBER
  139.             { $$ = make_redirection ( 0, r_duplicating, $2); }
  140.     |    NUMBER LESS_AND NUMBER
  141.             { $$ = make_redirection ($1, r_duplicating, $3); }
  142.     |    GREATER_AND NUMBER
  143.             { $$ = make_redirection ( 1, r_duplicating, $2); }
  144.     |    NUMBER GREATER_AND NUMBER
  145.             { $$ = make_redirection ($1, r_duplicating, $3); }
  146.     |    LESS_LESS_MINUS WORD
  147.             { $$ = make_redirection ( 0, r_deblank_reading_until, $2); }
  148.     |    NUMBER LESS_LESS_MINUS WORD
  149.             { $$ = make_redirection ($1, r_deblank_reading_until, $3); }
  150.     |    GREATER_AND '-'
  151.             { $$ = make_redirection ( 1, r_close_this, 0); }
  152.  
  153.     |    NUMBER GREATER_AND '-'
  154.             { $$ = make_redirection ($1, r_close_this, 0); }
  155.     |    LESS_AND '-'
  156.             { $$ = make_redirection ( 0, r_close_this, 0); }
  157.     |    NUMBER LESS_AND '-'
  158.             { $$ = make_redirection ($1, r_close_this, 0); }
  159.     |    AND_GREATER WORD
  160.             { $$ = make_redirection ( 1, r_err_and_out, $2); }
  161.     |    GREATER_AND WORD
  162.             { $$ = make_redirection ( 1, r_err_and_out, $2); }
  163.     ;
  164.  
  165. simple_command_element: WORD
  166.              { $$.word = $1; $$.redirect = 0; }
  167.     |    redirection
  168.             { $$.redirect = $1; $$.word = 0; }
  169.     ;
  170.  
  171. redirections:    redirection
  172.     |    redirections redirection
  173.             { $1->next = $2; $$ = $1; }
  174.     ;
  175.  
  176. simple_command:    simple_command_element
  177.             { $$ = make_simple_command ($1, NULL); }
  178.     |    simple_command simple_command_element
  179.             { $$ = make_simple_command ($2, $1); }
  180.     ;
  181.  
  182. command:    simple_command
  183.             { $$ = clean_simple_command ($1); }
  184.     |    shell_command
  185.             { $$ = $1; }
  186.  
  187.     |    shell_command redirections
  188.             { $$->redirects = $2; $$ = $1; }
  189.     ;
  190.  
  191. shell_command:    FOR WORD newlines DO list DONE
  192.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("$@", NULL), $5); }
  193.     |    FOR WORD ';' newlines DO list DONE
  194.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("$@", NULL), $6); }
  195.     |    FOR WORD ';' newlines '{' list '}'
  196.             { $$ = make_for_command ($2, (WORD_LIST *)add_string_to_list ("$@", NULL), $6); }
  197.     |    FOR WORD newlines IN words list_terminator newlines DO list DONE
  198.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  199.     |    FOR WORD newlines IN words list_terminator newlines '{' list '}'
  200.             { $$ = make_for_command ($2, (WORD_LIST *)reverse_list ($5), $9); }
  201.  
  202.     |    CASE WORD newlines IN newlines ESAC
  203.             { $$ = make_case_command ($2, NULL); }
  204.     |    CASE WORD newlines IN case_clause_sequence newlines ESAC
  205.             { $$ = make_case_command ($2, $5); }
  206.     |    CASE WORD newlines IN case_clause_1 ESAC
  207.             { report_syntax_error ("Inserted `;;'");
  208.               $$ = make_case_command ($2, $5); }
  209.  
  210.     |    IF list THEN list FI
  211.               { $$ = make_if_command ($2, $4, NULL); }
  212.     |    IF list THEN list ELSE list FI
  213.             { $$ = make_if_command ($2, $4, $6); }
  214.     |    IF list THEN list elif_clause FI
  215.             { $$ = make_if_command ($2, $4, $5); }
  216.  
  217.     |    WHILE list DO list DONE
  218.             { $$ = make_while_command ($2, $4); }
  219.     |    UNTIL list DO list DONE
  220.             { $$ = make_until_command ($2, $4); }
  221.  
  222.     |    '(' list ')'
  223.             { $2->subshell = 1; $$ = $2; }
  224.  
  225.     |    group_command
  226.             { $$ = $1; }
  227.  
  228.     |    WORD '(' ')' newlines group_command
  229.             { $$ = make_function_def ($1, $5); }
  230.  
  231.     |    FUNCTION WORD '(' ')' newlines group_command
  232.             { $$ = make_function_def ($2, $6); }
  233.  
  234.     |    FUNCTION WORD '\n' newlines group_command
  235.             { $$ = make_function_def ($2, $5); }
  236.  
  237.     |    FUNCTION WORD group_command
  238.             { $$ = make_function_def ($2, $3); }
  239.     ;
  240.  
  241. group_command:    '{' list '}'
  242.             { $$ = make_group_command ($2); }
  243.     ;
  244.  
  245. elif_clause:    ELIF list THEN list
  246.             { $$ = make_if_command ($2, $4, NULL); }
  247.     |    ELIF list THEN list ELSE list
  248.             { $$ = make_if_command ($2, $4, $6); }
  249.     |    ELIF list THEN list elif_clause
  250.             { $$ = make_if_command ($2, $4, $5); }
  251.     ;
  252.  
  253.  
  254. case_clause_1:    pattern_list_1
  255.     |    case_clause_sequence pattern_list_1
  256.               { $2->next = $1; $$ = $2; }
  257.     ;
  258.  
  259. pattern_list_1:    newlines pattern ')' list
  260.             { $$ = make_pattern_list ($2, $4); }
  261.     |    newlines pattern ')' newlines
  262.             { $$ = make_pattern_list ($2, NULL); }
  263.     ;
  264.  
  265. case_clause_sequence:  pattern_list
  266.  
  267.       |    case_clause_sequence pattern_list
  268.             { $2->next = $1; $$ = $2; }
  269.     ;
  270.  
  271. pattern_list:    newlines pattern ')' list SEMI_SEMI
  272.             { $$ = make_pattern_list ($2, $4); }
  273.     |    newlines pattern ')' newlines SEMI_SEMI
  274.             { $$ = make_pattern_list ($2, NULL); }
  275.     ;
  276.  
  277. pattern:    WORD
  278.             { $$ = make_word_list ($1, NULL); }
  279.     |    pattern '|' WORD
  280.             { $$ = make_word_list ($3, $1); }
  281.     ;
  282.  
  283. /* A list allows leading or trailing newlines and
  284.    newlines as operators (equivalent to semicolons).
  285.    It must end with a newline or semicolon.
  286.    Lists are used within commands such as if, for, while.  */
  287.  
  288. list:        newlines list0
  289.             { $$ = $2; }
  290.     ;
  291.  
  292. list0:        list1
  293.     |    list1 '\n' newlines
  294.     |    list1 '&' newlines
  295.             { $$ = command_connect ($1, 0, '&'); }
  296.     |    list1 ';' newlines
  297.  
  298.     ;
  299.  
  300. list1:        list1 AND_AND newlines list1
  301.             { $$ = command_connect ($1, $4, AND_AND); }
  302.     |    list1 OR_OR newlines list1
  303.             { $$ = command_connect ($1, $4, OR_OR); }
  304.     |    list1 '&' newlines list1
  305.             { $$ = command_connect ($1, $4, '&'); }
  306.     |    list1 ';' newlines list1
  307.             { $$ = command_connect ($1, $4, ';'); }
  308.     |    list1 '\n' newlines list1
  309.             { $$ = command_connect ($1, $4, ';'); }
  310.     |    list1 '|' newlines list1
  311.             { $$ = command_connect ($1, $4, '|'); }
  312.     |    command
  313.     ;
  314.  
  315.  
  316. list_terminator:'\n'
  317.     |    ';'
  318.     |    yacc_EOF
  319.     ;
  320.  
  321. newlines:
  322.     |    newlines '\n'
  323.     ;
  324.  
  325. /* A simple_list is a list that contains no significant newlines
  326.    and no leading or trailing newlines.  Newlines are allowed
  327.    only following operators, where they are not significant.
  328.  
  329.    This is what an inputunit consists of.  */
  330.  
  331. simple_list:    simple_list1
  332.     |    simple_list1 '&'
  333.             { $$ = command_connect ($1, (COMMAND *)NULL, '&'); }
  334.     |    simple_list1 ';'
  335.     ;
  336.  
  337. simple_list1:    simple_list1 AND_AND newlines simple_list1
  338.             { $$ = command_connect ($1, $4, AND_AND); }
  339.     |    simple_list1 OR_OR newlines simple_list1
  340.             { $$ = command_connect ($1, $4, OR_OR); }
  341.     |    simple_list1 '&' simple_list1
  342.             { $$ = command_connect ($1, $3, '&'); }
  343.     |    simple_list1 ';' simple_list1
  344.             { $$ = command_connect ($1, $3, ';'); }
  345.     |    simple_list1 '|' newlines simple_list1
  346.             { $$ = command_connect ($1, $4, '|'); }
  347.     |    command
  348.     ;
  349. %%
  350.  
  351. /* Initial size to allocate for tokens, and the
  352.    amount to grow them by. */
  353. #define TOKEN_DEFAULT_GROW_SIZE 512
  354.  
  355. /* The token currently being read. */
  356. int current_token = 0;
  357.  
  358. /* The last read token, or NULL.  read_token () uses this for context
  359.    checking. */
  360. int last_read_token = 0;
  361.  
  362. /* The token read prior to last_read_token. */
  363. int token_before_that = 0;
  364.  
  365. /* Global var is non-zero when end of file has been reached. */
  366. int EOF_Reached = 0;
  367.  
  368. /* yy_getc () returns the next available character from input or EOF.
  369.    yy_ungetc (c) makes `c' the next character to read.
  370.    init_yy_io (get, unget), makes the function `get' the installed function
  371.    for getting the next character, and makes `unget' the installed function
  372.    for un-getting a character. */
  373. return_EOF ()            /* does nothing good. */
  374. {
  375.   return (EOF);
  376. }
  377.  
  378. /* Variables containing the current get and unget functions. */
  379.  
  380. /* Some stream `types'. */
  381. #define st_stream 0
  382. #define st_string 1
  383.  
  384. Function *get_yy_char = return_EOF;
  385. Function *unget_yy_char = return_EOF;
  386. int yy_input_type = st_stream;
  387. FILE *yy_input_dev = (FILE *)NULL;
  388.  
  389. /* The current stream name.  In the case of a file, this is a filename. */
  390. char *stream_name = (char *)NULL;
  391.  
  392. /* Function to set get_yy_char and unget_yy_char. */
  393. init_yy_io (get_function, unget_function, type, location)
  394.      Function *get_function, *unget_function;
  395.      int type;
  396.      FILE *location;
  397. {
  398.   get_yy_char = get_function;
  399.   unget_yy_char = unget_function;
  400.   yy_input_type = type;
  401.   yy_input_dev = location;
  402. }
  403.  
  404. /* Call this to get the next character of input. */
  405. yy_getc ()
  406. {
  407.   return (*get_yy_char) ();
  408. }
  409.  
  410. /* Call this to unget C.  That is, to make C the next character
  411.    to be read. */
  412. yy_ungetc (c)
  413. {
  414.   return (*unget_yy_char) (c);
  415. }
  416.  
  417. /* **************************************************************** */
  418. /*                                    */
  419. /*          Let input be read from readline ().            */
  420. /*                                    */
  421. /* **************************************************************** */
  422.  
  423. #ifdef READLINE
  424. char *current_readline_prompt = (char *)NULL;
  425. char *current_readline_line = (char *)NULL;
  426. int current_readline_line_index = 0;
  427.  
  428. static int readline_initialized_yet = 0;
  429. yy_readline_get ()
  430. {
  431.   if (!current_readline_line)
  432.     {
  433.       char *readline ();
  434.       SigHandler *old_sigint;
  435.       extern sighandler throw_to_top_level ();
  436.       
  437.       if (!readline_initialized_yet)
  438.     {
  439.       initialize_readline ();
  440.       readline_initialized_yet = 1;
  441.     }
  442.  
  443.       old_sigint = (SigHandler *)signal (SIGINT, throw_to_top_level);
  444.       if (!current_readline_prompt)
  445.         current_readline_line = readline ("");
  446.       else
  447.         current_readline_line = readline (current_readline_prompt);
  448.       signal (SIGINT, old_sigint);
  449.  
  450.       current_readline_line_index = 0;
  451.  
  452.       if (!current_readline_line)
  453.     {
  454.       current_readline_line_index = 0;
  455.       return (EOF);
  456.     }
  457.  
  458.       current_readline_line =
  459.     (char *)xrealloc (current_readline_line,
  460.               2 + strlen (current_readline_line));
  461.       strcat (current_readline_line, "\n");
  462.     }
  463.  
  464.   if (!current_readline_line[current_readline_line_index])
  465.     {
  466.       free (current_readline_line);
  467.       current_readline_line = (char *)NULL;
  468.       return (yy_readline_get ());
  469.     }
  470.   else
  471.     {
  472.       int c = current_readline_line[current_readline_line_index++];
  473.       return (c);
  474.     }
  475. }
  476.  
  477. yy_readline_unget (c)
  478. {
  479.   if (current_readline_line_index && current_readline_line)
  480.     current_readline_line[--current_readline_line_index] = c;
  481. }
  482.   
  483. with_input_from_stdin ()
  484. {
  485.   init_yy_io (yy_readline_get, yy_readline_unget,
  486.           st_string, current_readline_line);
  487.   stream_name = savestring ("readline stdin");
  488. }
  489.  
  490. #else  /* READLINE */
  491.  
  492. with_input_from_stdin ()
  493. {
  494.   with_input_from_stream (stdin, "stdin");
  495. }
  496. #endif  /* READLINE */
  497.  
  498. /* **************************************************************** */
  499. /*                                    */
  500. /*   Let input come from STRING.  STRING is zero terminated.        */
  501. /*                                    */
  502. /* **************************************************************** */
  503.  
  504. yy_string_get ()
  505. {
  506.   /* If the string doesn't exist, or is empty, EOF found. */
  507.   if (!(char *)yy_input_dev || !*(char *)yy_input_dev)
  508.     return (EOF);
  509.   else {
  510.     register char *temp = (char *)yy_input_dev;
  511.     int c = *temp++;
  512.     yy_input_dev = (FILE *)temp;
  513.     return (c);
  514.   }
  515. }
  516.  
  517. yy_string_unget (c)
  518.      int c;
  519. {
  520.   register char *temp = (char *)yy_input_dev;
  521.   *(--temp) = c;
  522.   yy_input_dev = (FILE *)temp;
  523.   return (c);
  524. }
  525.  
  526. with_input_from_string (string, name)
  527.      char *string;
  528.      char *name;
  529. {
  530.   init_yy_io (yy_string_get, yy_string_unget, st_string, string);
  531.   stream_name = savestring (name);
  532. }
  533.  
  534. /* **************************************************************** */
  535. /*                                    */
  536. /*             Let input come from STREAM.            */
  537. /*                                    */
  538. /* **************************************************************** */
  539.  
  540. yy_stream_get ()
  541. {
  542.   if (yy_input_dev)
  543. #ifdef SYSV
  544.     return (sysv_getc (yy_input_dev));
  545. #else
  546.     return (getc (yy_input_dev));
  547. #endif  /* SYSV */
  548.   else return (EOF);
  549. }
  550.  
  551. yy_stream_unget (c)
  552.      int c;
  553. {
  554.   ungetc (c, yy_input_dev);
  555. }
  556.  
  557. with_input_from_stream (stream, name)
  558.      FILE *stream;
  559.      char *name;
  560. {
  561.   init_yy_io (yy_stream_get, yy_stream_unget, st_stream, stream);
  562.   stream_name = savestring (name);
  563. }
  564.  
  565. typedef struct stream_saver {
  566.   struct stream_saver *next;
  567.   Function *getter, *putter;
  568.   int type, line;
  569.   char *location, *name;
  570. } STREAM_SAVER;
  571.  
  572. /* The globally known line number. */
  573. int line_number = 0;
  574.  
  575. STREAM_SAVER *stream_list = (STREAM_SAVER *)NULL;
  576.  
  577. push_stream ()
  578. {
  579.   STREAM_SAVER *temp = (STREAM_SAVER *)xmalloc (sizeof (STREAM_SAVER));
  580.   temp->type = yy_input_type;
  581.   temp->location = (char *)yy_input_dev;
  582.   temp->getter = get_yy_char;
  583.   temp->putter = unget_yy_char;
  584.   temp->line = line_number;
  585.   temp->name = stream_name; stream_name = (char *)NULL;
  586.   temp->next = stream_list;
  587.   stream_list = temp;
  588.   EOF_Reached = line_number = 0;
  589. }
  590.  
  591. pop_stream ()
  592. {
  593.   if (!stream_list)
  594.     {
  595.       EOF_Reached = 1;
  596.     }
  597.   else
  598.     {
  599.       STREAM_SAVER *temp = stream_list;
  600.     
  601.       EOF_Reached = 0;
  602.       stream_list = stream_list->next;
  603.  
  604.       if (stream_name)
  605.     free (stream_name);
  606.       stream_name = temp->name;
  607.  
  608.       init_yy_io (temp->getter, temp->putter, temp->type, temp->location);
  609.       line_number = temp->line;
  610.       free (temp);
  611.     }
  612. }
  613.  
  614.  
  615. /* Return a line of text, taken from wherever yylex () reads input.
  616.    If there is no more input, then we return NULL. */
  617. char *
  618. read_a_line ()
  619. {
  620.   char *line_buffer = (char *)NULL;
  621.   int index = 0, buffer_size = 0;
  622.   int c;
  623.  
  624.   while (c = yy_getc ())
  625.     {
  626.       /* If there is no more input, then we return NULL. */
  627.       if (c == EOF)
  628.     {
  629.       c = '\n';
  630.       if (!line_buffer)
  631.         return ((char *)NULL);
  632.     }
  633.  
  634.       if (index + 1 > buffer_size)
  635.     if (!buffer_size)
  636.       line_buffer = (char *)xmalloc (buffer_size = 200);
  637.     else
  638.       line_buffer = (char *)xrealloc (line_buffer, buffer_size += 200);
  639.  
  640.       line_buffer[index++] = c;
  641.       if (c == '\n')
  642.     {
  643.       line_buffer[index] = '\0';
  644.       return (line_buffer);
  645.     }
  646.     }
  647. }
  648.  
  649.  
  650. /* Return a line as in read_a_line (), but insure that the prompt is
  651.    the secondary prompt. */
  652. char *
  653. read_secondary_line ()
  654. {
  655.   char *decode_prompt_string ();
  656.   char *temp_prompt = get_string_value ("PS2");
  657.  
  658. #ifdef READLINE
  659.   if (!no_line_editing)
  660.     {
  661.       extern char *current_readline_prompt;
  662.  
  663.       if (current_readline_prompt)
  664.     {
  665.       free (current_readline_prompt);
  666.       current_readline_prompt = (char *)NULL;
  667.     }
  668.  
  669.       if (temp_prompt)
  670.     current_readline_prompt = decode_prompt_string (temp_prompt);
  671.       else
  672.     current_readline_prompt = (savestring (""));
  673.     }
  674.   else
  675. #endif  /* READLINE */
  676.     {
  677.       printf ("%s", temp_prompt ? temp_prompt : "");
  678.       fflush (stdout);
  679.     }
  680.  
  681.   return (read_a_line ());
  682. }
  683.  
  684. /* **************************************************************** */
  685. /*                                    */
  686. /*                YYLEX ()                */
  687. /*                                    */
  688. /* **************************************************************** */
  689.  
  690. /* Reserved words.  These are only recognized as the first word of a
  691.    command.  TOKEN_WORD_ALIST. */
  692. struct {
  693.   char *word;
  694.   int token;
  695. } token_word_alist[] = {
  696.   {"if", IF},
  697.   {"then", THEN},
  698.   {"else", ELSE},
  699.   {"elif", ELIF},
  700.   {"fi", FI},
  701.   {"case", CASE},
  702.   {"esac", ESAC},
  703.   {"for", FOR},
  704.   {"while", WHILE},
  705.   {"until", UNTIL},
  706.   {"do", DO},
  707.   {"done", DONE},
  708.   {"in", IN},
  709.   {"function", FUNCTION},
  710.   {"{", '{'},
  711.   {"}", '}'},
  712.   {(char *)NULL, 0}
  713. };
  714.  
  715. /* Where shell input comes from.  For each line that we read, alias
  716.    and history expansion are done. */
  717. char *shell_input_line = (char *)NULL;
  718. int shell_input_line_index = 0;
  719. int shell_input_line_size = 0;
  720.  
  721. /* Either zero, or EOF. */
  722. int shell_input_line_terminator = 0;
  723.  
  724. /* Return the next shell input character.  This always reads characters
  725.    from shell_input_line; when that line is exhausted, it is time to
  726.    read the next line. */
  727. int
  728. shell_getc (remove_quoted_newline)
  729.      int remove_quoted_newline;
  730. {
  731.   extern int login_shell;
  732.   int c;
  733.  
  734.   if (!shell_input_line || !shell_input_line[shell_input_line_index])
  735.     {
  736.       register int i, l;
  737.       char *pre_process_line (), *expansions;
  738.  
  739.       restart_read_next_line:
  740.  
  741.       line_number++;
  742.  
  743.     restart_read:
  744.  
  745.       i = 0;
  746.       shell_input_line_terminator = 0;
  747.  
  748. #ifdef JOB_CONTROL
  749.       notify_and_cleanup ();
  750. #endif
  751.  
  752.       clearerr (stdin);
  753.       while (c = yy_getc ())
  754.     {
  755.       if (i + 2 > shell_input_line_size)
  756.         if (!shell_input_line)
  757.           shell_input_line = (char *)xmalloc (shell_input_line_size = 256);
  758.         else
  759.           shell_input_line =
  760.         (char *)xrealloc (shell_input_line, shell_input_line_size += 256);
  761.  
  762.       if (c == EOF)
  763.         {
  764.           clearerr (stdin);
  765.  
  766.           if (!i)
  767.         shell_input_line_terminator = EOF;
  768.  
  769.           shell_input_line[i] = '\0';
  770.           break;
  771.         }
  772.  
  773.       shell_input_line[i++] = c;
  774.  
  775.       if (c == '\n')
  776.         {
  777.           shell_input_line[--i] = '\0';
  778.           break;
  779.         }
  780.     }
  781.       shell_input_line_index = 0;
  782.  
  783.       if (!shell_input_line[0] || shell_input_line[0] == '#')
  784.     goto after_pre_process;
  785.  
  786.       expansions = pre_process_line (shell_input_line, 1, 1);
  787.  
  788.       free (shell_input_line);
  789.       shell_input_line = expansions;
  790.  
  791.       if (shell_input_line)
  792.     {
  793.       if (echo_input_at_read)
  794.         fprintf (stderr, "%s\n", shell_input_line);
  795.  
  796.       shell_input_line_size = strlen (expansions);
  797.  
  798.     }
  799.       else
  800.     {
  801.       shell_input_line_size = 0;
  802.       prompt_again ();
  803.       goto restart_read;
  804.     }
  805.  
  806.     after_pre_process:
  807.       /* Add the newline to the end of this string, iff the string does
  808.      not already end in an EOF character.  */
  809.       if (shell_input_line_terminator != EOF)
  810.     {
  811.       l = strlen (shell_input_line);
  812.  
  813.       if (l + 3 > shell_input_line_size)
  814.         shell_input_line =
  815.           (char *)xrealloc (shell_input_line,
  816.                 1 + (shell_input_line_size += 2));
  817.       strcpy (shell_input_line + l, "\n");
  818.     }
  819.     }
  820.   
  821.   c = shell_input_line[shell_input_line_index];
  822.  
  823.   if (c)
  824.     shell_input_line_index++;
  825.  
  826.   if (c == '\\' && remove_quoted_newline &&
  827.       shell_input_line[shell_input_line_index] == '\n')
  828.     {
  829.       prompt_again ();
  830.       goto restart_read_next_line;
  831.     }
  832.  
  833.   if (!c && shell_input_line_terminator == EOF)
  834.     {
  835.       if (shell_input_line_index != 0)
  836.     return ('\n');
  837.       else
  838.     return (EOF);
  839.     }
  840.  
  841.   return (c);
  842. }
  843.  
  844. /* Put C back into the input for the shell. */
  845. shell_ungetc (c)
  846.      int c;
  847. {
  848.   if (shell_input_line && shell_input_line_index)
  849.     shell_input_line[--shell_input_line_index] = c;
  850. }
  851.  
  852. /* Discard input until CHARACTER is seen. */
  853. discard_until (character)
  854.      int character;
  855. {
  856.   int c;
  857.   while ((c = shell_getc (0)) != EOF && c != character)
  858.     ;
  859.   if (c != EOF )
  860.     shell_ungetc (c);
  861. }
  862.  
  863. /* Tell readline () that we have some text for it to edit. */
  864. re_edit (text)
  865.      char *text;
  866. {
  867. #ifdef READLINE
  868.   if (strcmp (stream_name, "readline stdin") == 0)
  869.     bash_re_edit (text);
  870. #endif
  871. }
  872.  
  873. /* Non-zero means do no history expansion on this line, regardless
  874.    of what history_expansion says. */
  875. int history_expansion_inhibited = 0;
  876.  
  877. /* Do pre-processing on LINE.  If PRINT_CHANGES is non-zero, then
  878.    print the results of expanding the line if there were any changes.
  879.    If there is an error, return NULL, otherwise the expanded line is
  880.    returned.  If ADDIT is non-zero the line is added to the history
  881.    list after history expansion, but before alias expansion.  ADDIT
  882.    is just a suggestion; REMEMBER_ON_HISTORY can veto, and does.
  883.    Right now this does history and alias expansion. */
  884. char *
  885. pre_process_line (line, print_changes, addit)
  886.      char *line;
  887.      int print_changes, addit;
  888. {
  889.   extern int history_expansion;
  890.   extern int remember_on_history;
  891.   int history_expand ();
  892.   char *history_value;
  893.   char *return_value = line;
  894.   int expanded = 0;
  895.  
  896. #ifdef ALIAS
  897.   char *alias_expand (), *alias_value;
  898. #endif
  899.  
  900.   /* History expand the line.  If this results in no errors, then
  901.      add that line to the history if ADDIT is non-zero. */
  902.   if (!history_expansion_inhibited && history_expansion)
  903.     {
  904.       expanded = history_expand (line, &history_value);
  905.  
  906.       if (expanded)
  907.     {
  908.       if (print_changes)
  909.         fprintf (stderr, "%s\n", history_value);
  910.  
  911.       /* If there was an error, return NULL. */
  912.       if (expanded < 0)
  913.         {
  914.           free (history_value);
  915.  
  916.           /* New hack.  We can allow the user to edit the
  917.          failed history expansion. */
  918.           re_edit (line);
  919.  
  920.           return ((char *)NULL);
  921.         }
  922.     }
  923.  
  924.       /* Let other expansions know that return_value can be free'ed,
  925.      and that a line has been added to the history list. */
  926.       expanded = 1;
  927.       return_value = history_value;
  928.     }
  929.  
  930.   if (addit && remember_on_history)
  931.     {
  932.       extern int history_control;
  933.  
  934.       switch (history_control)
  935.     {
  936.     case 0:
  937.       add_history (return_value);
  938.       break;
  939.     case 1:
  940.       if (*return_value != ' ')
  941.         add_history (return_value);
  942.       break;
  943.     case 2:
  944.       {
  945.         HIST_ENTRY *temp = previous_history ();
  946.         if (!temp || (strcmp (temp->line, return_value) != 0))
  947.           add_history (return_value);
  948.         using_history ();
  949.       }
  950.       break;
  951.     }
  952.     }
  953.  
  954. #ifdef ALIAS
  955.   alias_value = alias_expand (return_value);
  956.  
  957.   if (expanded)
  958.     {
  959.       expanded = 0;
  960.       free (return_value);
  961.     }
  962.  
  963.   return_value = alias_value;
  964.  
  965. #else
  966.   return_value = savestring (line);
  967. #endif /* ALIAS */
  968.  
  969. #ifdef ALIAS
  970. #ifdef NEVER  /* Expanding history-wise here is sematically incorrect
  971.          for this shell, and should never be done.  I figured
  972.          it out, so just trust me, okay? */
  973.   /* History expand the alias.  This is a special hack.  Don't you
  974.      just hate this? */
  975.  
  976.   if (!history_expansion_inhibited && history_expansion)
  977.     {
  978.       expanded = history_expand (return_value, &history_value);
  979.  
  980.       if (expanded < 0)
  981.     {
  982.       free (history_value);
  983.       free (return_value);
  984.       return ((char *)NULL);
  985.     }
  986.  
  987.       if (expanded)
  988.     {
  989.       free (return_value);
  990.       return_value = history_value;
  991.     }
  992.     }
  993. #endif  /* NEVER */
  994. #endif  /* ALIAS */
  995.   return (return_value);
  996. }
  997.  
  998.  
  999. /* Place to remember the token.  We try to keep the buffer
  1000.    at a reasonable size, but it can grow. */
  1001. char *token = NULL;
  1002.  
  1003. /* Current size of the token buffer. */
  1004. int token_buffer_size = 0;
  1005.  
  1006. /* Command to read_token () explaining what we want it to do. */
  1007. #define READ 0
  1008. #define RESET 1
  1009.  
  1010. /* prompt_string_pointer points to one of these,
  1011.    never to an actual string. */
  1012. char *ps1_prompt, *ps2_prompt;
  1013.  
  1014. /* Handle on the current prompt string.  Indirectly points through
  1015.    ps1_ or ps2_prompt. */
  1016. char **prompt_string_pointer;
  1017.  
  1018. /* Function for yyparse to call.  yylex keeps track of
  1019.    the last two tokens read, and calls read_token.  */
  1020. yylex ()
  1021. {
  1022.   if (interactive && (!current_token || current_token == '\n'))
  1023.     {
  1024.       /* Before we print a prompt, we might have to check mailboxes.
  1025.      We do this only if it is time to do so. Notice that only here
  1026.      is the mail alarm reset; nothing takes place in check_mail ()
  1027.      except the checking of mail.  Please don't change this. */
  1028.       if (time_to_check_mail ())
  1029.     {
  1030.       check_mail ();
  1031.       reset_mail_timer ();
  1032.     }
  1033.  
  1034.       /* Allow the execution of a random command just before the printing
  1035.      of each prompt.  If the shell variable PROMPT_COMMAND
  1036.      is set then the value of it is the command to execute. */
  1037.       {
  1038.     char *command_to_execute = get_string_value ("PROMPT_COMMAND");
  1039.  
  1040.     if (command_to_execute)
  1041.       {
  1042.         extern Function *last_shell_builtin, *this_shell_builtin;
  1043.         Function *temp_last, *temp_this;
  1044.  
  1045.         temp_last = last_shell_builtin;
  1046.         temp_this = this_shell_builtin;
  1047.  
  1048.         parse_and_execute (savestring (command_to_execute),
  1049.                    "PROMPT_COMMAND");
  1050.         last_shell_builtin = temp_last;
  1051.         this_shell_builtin = temp_this;
  1052.       }
  1053.       }
  1054.  
  1055.       prompt_again ();
  1056.       prompt_string_pointer = &ps2_prompt;
  1057.     }
  1058.  
  1059.   token_before_that = last_read_token;
  1060.   last_read_token = current_token;
  1061.   current_token = read_token (READ);
  1062.   return (current_token);
  1063. }
  1064.  
  1065. /* Called from shell.c when Control-C is typed at top level.  Or
  1066.    by the error rule at top level. */
  1067. reset_parser ()
  1068. {
  1069.   read_token (RESET);
  1070. }
  1071.   
  1072. /* When non-zero, we have read the required tokens
  1073.    which allow ESAC to be the next one read. */
  1074. static int allow_esac_as_next = 0;
  1075.  
  1076. /* When non-zero, accept single '{' as a token itself. */
  1077. static int allow_open_brace = 0;
  1078.  
  1079. /* DELIMITER is the value of the delimiter that is currently
  1080.    enclosing, or zero for none. */
  1081. static int delimiter = 0;
  1082.  
  1083. /* When non-zero, an open-brace used to create a group is awaiting a close
  1084.    brace partner. */
  1085. static int open_brace_awaiting_satisfaction = 0;
  1086.  
  1087. /* If non-zero, it is the token that we want read_token to return regardless
  1088.    of what text is (or isn't) present to be read.  read_token resets this. */
  1089. int token_to_read = 0;
  1090.  
  1091. /* Read the next token.  Command can be READ (normal operation) or 
  1092.    RESET (to normalize state. */
  1093. read_token (command)
  1094.      int command;
  1095. {
  1096.   int character;        /* Current character. */
  1097.   int peek_char;        /* Temporary look-ahead character. */
  1098.   int result;            /* The thing to return. */
  1099.   WORD_DESC *the_word;        /* The value for YYLVAL when a WORD is read. */
  1100.  
  1101.   if (token_buffer_size < TOKEN_DEFAULT_GROW_SIZE)
  1102.     {
  1103.       if (token)
  1104.     free (token);
  1105.       token = (char *)xmalloc (token_buffer_size = TOKEN_DEFAULT_GROW_SIZE);
  1106.     }
  1107.  
  1108.   if (command == RESET)
  1109.     {
  1110.       delimiter = 0;
  1111.       open_brace_awaiting_satisfaction = 0;
  1112.       if (shell_input_line)
  1113.     {
  1114.       free (shell_input_line);
  1115.       shell_input_line = (char *)NULL;
  1116.       shell_input_line_size = 0;
  1117.     }
  1118.       last_read_token = '\n';
  1119.       token_to_read = '\n';
  1120.       return;
  1121.     }
  1122.  
  1123.   if (token_to_read)
  1124.     {
  1125.       int rt = token_to_read;
  1126.       token_to_read = 0;
  1127.       return (rt);
  1128.     }
  1129.  
  1130.   /* Read a single word from input.  Start by skipping blanks. */
  1131.   while ((character = shell_getc (1)) != EOF && whitespace (character));
  1132.  
  1133.   if (character == EOF)
  1134.     return (yacc_EOF);
  1135.  
  1136.   if (character == '#' && !interactive)
  1137.     {
  1138.       /* A comment.  Discard until EOL or EOF, and then return a newline. */
  1139.       discard_until ('\n');
  1140.       shell_getc (0);
  1141.       return ('\n');
  1142.     }
  1143.  
  1144.   if (character == '\n')
  1145.     return (character);
  1146.  
  1147.   if (member (character, "()<>;&|"))
  1148.     {
  1149.       /* Please note that the shell does not allow whitespace to
  1150.      appear in between tokens which are character pairs, such as
  1151.      "<<" or ">>".  I believe this is the correct behaviour. */
  1152.  
  1153.       if (character == (peek_char = shell_getc (1)))
  1154.     {
  1155.       switch (character)
  1156.         {
  1157.           /* If '<' then we could be at "<<" or at "<<-".  We have to
  1158.          look ahead one more character. */
  1159.         case '<':
  1160.           peek_char = shell_getc (1);
  1161.           if (peek_char == '-')
  1162.         return (LESS_LESS_MINUS);
  1163.           else
  1164.         {
  1165.           shell_ungetc (peek_char);
  1166.           return (LESS_LESS);
  1167.         }
  1168.  
  1169.         case '(': return (DOUBLE_OPEN);
  1170.         case ')': return (DOUBLE_CLOSE);
  1171.         case '>': return (GREATER_GREATER);
  1172.         case ';':    return (SEMI_SEMI);
  1173.         case '&': return (AND_AND);
  1174.         case '|': return (OR_OR);
  1175.         }
  1176.     }
  1177.       else
  1178.     {
  1179.       if (peek_char == '&')
  1180.         {
  1181.           switch (character)
  1182.         {
  1183.         case '<': return (LESS_AND);
  1184.         case '>': return (GREATER_AND);
  1185.         }
  1186.         }
  1187.       if (peek_char == '>' && character == '&')
  1188.         return (AND_GREATER);
  1189.     }
  1190.       shell_ungetc (peek_char);
  1191.  
  1192.       /* If we look like we are reading the start of a function
  1193.      definition, then let the reader know about it so that
  1194.      we will do the right thing with `{'. */
  1195.       if (character == ')' &&
  1196.       last_read_token == '(' && token_before_that == WORD)
  1197.     allow_open_brace = 1;
  1198.  
  1199.       return (character);
  1200.     }
  1201.  
  1202.   /* Hack <&- (close stdin) case. */
  1203.   if (character == '-')
  1204.     {
  1205.       switch (last_read_token)
  1206.     {
  1207.     case LESS_AND:
  1208.     case GREATER_AND:
  1209.       return (character);
  1210.     }
  1211.     }
  1212.   
  1213.   /* Okay, if we got this far, we have to read a word.  Read one,
  1214.      and then check it against the known ones. */
  1215.   {
  1216.     /* Index into the token that we are building. */
  1217.     int token_index = 0;
  1218.  
  1219.     /* ALL_DIGITS becomes zero when we see a non-digit. */
  1220.     int all_digits = digit (character);
  1221.  
  1222.     /* DOLLAR_PRESENT becomes non-zero if we see a `$'. */
  1223.     int dollar_present = 0;
  1224.  
  1225.     /* QUOTED becomes non-zero if we see one of ("), ('), (`), or (\). */
  1226.     int quoted = 0;
  1227.  
  1228.     /* Non-zero means to ignore the value of the next character, and just
  1229.        to add it no matter what. */
  1230.     int pass_next_character = 0;
  1231.  
  1232.     /* Non-zero means parsing a dollar-paren construct.  It is the count of
  1233.        un-quoted closes we need to see. */
  1234.     int dollar_paren_level = 0;
  1235.  
  1236.     /* Another level variable.  This one is for dollar_parens inside of
  1237.        double-quotes. */
  1238.     int delimited_paren_level = 0;
  1239.  
  1240.     for (;;)
  1241.       {
  1242.     if (character == EOF)
  1243.       goto got_token;
  1244.  
  1245.     if (pass_next_character)
  1246.       {
  1247.         pass_next_character = 0;
  1248.         goto got_character;
  1249.       }
  1250.  
  1251.     /* Handle double backslash.  These are always magic.  The
  1252.        second backslash does not cause a trailing newline to be
  1253.        eaten. */
  1254.  
  1255.     if (character == '\\')
  1256.       {
  1257.         peek_char = shell_getc (0);
  1258.         if (peek_char != '\\')
  1259.           shell_ungetc (peek_char);
  1260.         else
  1261.           {
  1262.         token[token_index++] = character;
  1263.         goto got_character;
  1264.           }
  1265.       }
  1266.  
  1267.     /* Handle backslashes.  Quote lots of things when not inside of
  1268.        double-quotes, quote some things inside of double-quotes. */
  1269.  
  1270.     if (character == '\\' && delimiter != '\'')
  1271.       {
  1272.         peek_char = shell_getc (0);
  1273.  
  1274.         /* Backslash-newline is ignored in all other cases. */
  1275.         if (peek_char == '\n')
  1276.           {
  1277.         character = '\n';
  1278.         goto next_character;
  1279.           }
  1280.         else
  1281.           {
  1282.         shell_ungetc (peek_char);
  1283.  
  1284.         /* If the next character is to be quoted, do it now. */
  1285.         if (!delimiter || delimiter == '`' ||
  1286.             ((delimiter == '"' ) &&
  1287.              (member (peek_char, slashify_in_quotes))))
  1288.           {
  1289.             pass_next_character++;
  1290.             quoted = 1;
  1291.             goto got_character;
  1292.           }
  1293.           }
  1294.       }
  1295.  
  1296.     if (delimiter)
  1297.       {
  1298.         if (character == delimiter)
  1299.           {
  1300.         delimiter = 0;
  1301.         if (delimited_paren_level)
  1302.           {
  1303.             report_error ("Expected ')' before %c", character);
  1304.             return ('\n');
  1305.           }
  1306.         goto got_character;
  1307.           }
  1308.       }
  1309.  
  1310.     if (!delimiter || delimiter == '`' || delimiter == '"')
  1311.       {
  1312.         if (character == '$')
  1313.           {
  1314.         peek_char = shell_getc (1);
  1315.         shell_ungetc (peek_char);
  1316.         if (peek_char == '(')
  1317.           {
  1318.             if (!delimiter)
  1319.               dollar_paren_level++;
  1320.             else
  1321.               delimited_paren_level++;
  1322.  
  1323.             pass_next_character++;
  1324.             goto got_character;
  1325.           }
  1326.           }
  1327.         
  1328.         if (character == ')')
  1329.           {
  1330.         if (delimiter && delimited_paren_level)
  1331.           delimited_paren_level--;
  1332.  
  1333.         if (!delimiter && dollar_paren_level)
  1334.           {
  1335.             dollar_paren_level--;
  1336.             goto got_character;
  1337.           }
  1338.           }
  1339.       }
  1340.  
  1341.     if (!dollar_paren_level && !delimiter &&
  1342.         member (character, " \t\n;&()|<>"))
  1343.       {
  1344.         shell_ungetc (character);
  1345.         goto got_token;
  1346.       }
  1347.     
  1348.     if (!delimiter)
  1349.       {
  1350.         if (character == '"' || character == '`' || character == '\'')
  1351.           {
  1352.         quoted = 1;
  1353.         delimiter = character;
  1354.         goto got_character;
  1355.           }
  1356.       }
  1357.  
  1358.     if (all_digits) all_digits = digit (character);
  1359.     if (character == '$') dollar_present = 1;
  1360.  
  1361.       got_character:
  1362.  
  1363.     token[token_index++] = character;
  1364.  
  1365.     if (token_index == (token_buffer_size - 1))
  1366.       token = (char *)xrealloc (token, (token_buffer_size
  1367.                         += TOKEN_DEFAULT_GROW_SIZE));
  1368.     {
  1369.       char *decode_prompt_string ();
  1370.  
  1371.     next_character:
  1372.       if (character == '\n' && interactive)
  1373.         prompt_again ();
  1374.     }
  1375.     character = shell_getc ((delimiter != '\''));
  1376.       }
  1377.  
  1378. got_token:
  1379.  
  1380.     token[token_index] = '\0';
  1381.     
  1382.     if ((delimiter || dollar_paren_level) && character == EOF)
  1383.       {
  1384.     if (dollar_paren_level && !delimiter)
  1385.       delimiter = ')';
  1386.  
  1387.     report_error ("Unexpected EOF.  Looking for `%c'.", delimiter);
  1388.     return (-1);
  1389.       }
  1390.  
  1391.     if (all_digits)
  1392.       {
  1393.     /* Check to see what thing we should return.  If the last_read_token
  1394.        is a `<', or a `&', or the character which ended this token is
  1395.        a '>' or '<', then, and ONLY then, is this input token a NUMBER.
  1396.        Otherwise, it is just a word, and should be returned as such. */
  1397.  
  1398.     if ((character == '<' || character == '>') ||
  1399.         (last_read_token == LESS_AND ||
  1400.          last_read_token == GREATER_AND))
  1401.       {
  1402.         sscanf (token, "%d", &(yylval.number));
  1403.         return (NUMBER);
  1404.       }
  1405.       }
  1406.  
  1407.     /* Handle special case.  IN is recognized if the last token
  1408.        was WORD and the token before that was FOR or CASE. */
  1409.     if ((strcmp (token, "in") == 0) &&
  1410.     (last_read_token == WORD) &&
  1411.     ((token_before_that == FOR) ||
  1412.      (token_before_that == CASE)))
  1413.       {
  1414.     if (token_before_that == CASE) allow_esac_as_next++;
  1415.     return (IN);
  1416.       }
  1417.  
  1418.     /* Ditto for DO in the FOR case. */
  1419.     if ((strcmp (token, "do") == 0) &&
  1420.     (last_read_token == WORD) &&
  1421.     (token_before_that == FOR))
  1422.       return (DO);
  1423.  
  1424.     /* Ditto for ESAC in the CASE case. 
  1425.        Specifically, this handles "case word in esac", which is a legal
  1426.        construct, certainly because someone will pass an empty arg to the
  1427.        case construct, and we don't want it to barf.  Of course, we should
  1428.        insist that the case construct has at least one pattern in it, but
  1429.        the designers disagree. */
  1430.     if (allow_esac_as_next)
  1431.       {
  1432.     allow_esac_as_next--;
  1433.     if (strcmp (token, "esac") == 0)
  1434.       return (ESAC);
  1435.       }
  1436.  
  1437.     /* Ditto for `{' in the FUNCTION case. */
  1438.     if (allow_open_brace)
  1439.       {
  1440.     allow_open_brace = 0;
  1441.     if (strcmp (token, "{") == 0)
  1442.       {
  1443.         open_brace_awaiting_satisfaction++;
  1444.         return ('{');
  1445.       }
  1446.       }
  1447.  
  1448.     /* Check to see if it is a reserved word. */
  1449.     if (!dollar_present && !quoted &&
  1450.     reserved_word_acceptable (last_read_token))
  1451.       {
  1452.     int i;
  1453.     for (i = 0; token_word_alist[i].word != (char *)NULL; i++)
  1454.       if (strcmp (token, token_word_alist[i].word) == 0)
  1455.         {
  1456.           if (token_word_alist[i].token == '{')
  1457.         open_brace_awaiting_satisfaction++;
  1458.  
  1459.           return (token_word_alist[i].token);
  1460.         }
  1461.       }
  1462.  
  1463.     /* What if we are attempting to satisfy an open-brace grouper? */
  1464.     if (open_brace_awaiting_satisfaction && strcmp (token, "}") == 0)
  1465.       {
  1466.     open_brace_awaiting_satisfaction--;
  1467.     return ('}');
  1468.       }
  1469.       
  1470.     the_word = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  1471.     the_word->word = (char *)xmalloc (1 + strlen (token));
  1472.     strcpy (the_word->word, token);
  1473.     the_word->dollar_present = dollar_present;
  1474.     the_word->quoted = quoted;
  1475.     the_word->assignment = assignment (token);
  1476.  
  1477.     yylval.word = the_word;
  1478.     result = WORD;
  1479.     if (last_read_token == FUNCTION)
  1480.       allow_open_brace = 1;
  1481.   }
  1482.   return (result);
  1483. }
  1484.  
  1485. /* Return 1 if TOKEN is a token that after being read would allow
  1486.    a reserved word to be seen, else 0. */
  1487. reserved_word_acceptable (token)
  1488.      int token;
  1489. {
  1490.   if (member (token, "\n;()|&{") ||
  1491.       token == AND_AND ||
  1492.       token == OR_OR ||
  1493.       token == SEMI_SEMI ||
  1494.       token == DO ||
  1495.       token == IF ||
  1496.       token == THEN ||
  1497.       token == ELSE ||
  1498.       token == ELIF ||
  1499.       token == 0)
  1500.     return (1);
  1501.   else
  1502.     return (0);
  1503. }
  1504.  
  1505. /* Issue a prompt, or prepare to issue a prompt when the next character
  1506.    is read. */
  1507. prompt_again ()
  1508. {
  1509.   char *decode_prompt_string ();
  1510.   char *temp_prompt;
  1511.  
  1512.   ps1_prompt = get_string_value ("PS1");
  1513.   ps2_prompt = get_string_value ("PS2");
  1514.  
  1515.   if (!prompt_string_pointer)
  1516.     prompt_string_pointer = &ps1_prompt;
  1517.  
  1518.   if (*prompt_string_pointer)
  1519.     temp_prompt = decode_prompt_string (*prompt_string_pointer);
  1520.   else
  1521.     temp_prompt = savestring ("");
  1522.  
  1523. #ifdef READLINE
  1524.   if (!no_line_editing)
  1525.     {
  1526.       if (current_readline_prompt)
  1527.     free (current_readline_prompt);
  1528.       
  1529.       current_readline_prompt = temp_prompt;
  1530.     }
  1531.   else
  1532. #endif  /* READLINE */
  1533.     {
  1534.       if (interactive)
  1535.     {
  1536.       fprintf (stderr, "%s", temp_prompt);
  1537.       fflush (stderr);
  1538.     }
  1539.       free (temp_prompt);
  1540.     }
  1541. }
  1542.  
  1543. /* This sucks. but it is just a crock for SYSV.  The whole idea of MAXPATHLEN
  1544.    is a crock if you ask me.  Why can't we just have dynamically defined
  1545.    sizes?  (UCSB crashes every 20 minutes on me.) */
  1546. #ifndef MAXPATHLEN
  1547. #define MAXPATHLEN 1024
  1548. #endif  /* MAXPATHLEN */
  1549.  
  1550. /* Return a string which will be printed as a prompt.  The string
  1551.    may contain special characters which are decoded as follows:
  1552.    
  1553.     \t    the time
  1554.     \d    the date
  1555.     \n    CRLF
  1556.     \s    the name of the shell
  1557.     \w    the current working directory
  1558.     \W    the last element of PWD
  1559.     \u    your username
  1560.     \h    the hostname
  1561.     \#    the command number of this command
  1562.     \!    the history number of this command
  1563.     \$    a $ or a # if you are root
  1564.     \<octal> character code in octal
  1565.     \\    a backslash
  1566. */
  1567. #include <sys/param.h>
  1568. #include <time.h>
  1569.  
  1570. #define PROMPT_GROWTH 50
  1571. char *
  1572. decode_prompt_string (string)
  1573.      char *string;
  1574. {
  1575.   int result_size = PROMPT_GROWTH;
  1576.   int result_index = 0;
  1577.   char *result = (char *)xmalloc (PROMPT_GROWTH);
  1578.   int c;
  1579.   char *temp = (char *)NULL;
  1580.  
  1581.   result[0] = 0;
  1582.   while (c = *string++)
  1583.     {
  1584.       if (c == '\\')
  1585.     {
  1586.       c = *string;
  1587.  
  1588.       switch (c)
  1589.         {
  1590.  
  1591.         case '0':
  1592.         case '1':
  1593.         case '2':
  1594.         case '3':
  1595.         case '4':
  1596.         case '5':
  1597.         case '6':
  1598.         case '7':
  1599.           {
  1600.         char octal_string[4];
  1601.         int n;
  1602.  
  1603.         strncpy (octal_string, string, 3);
  1604.         octal_string[3] = '\0';
  1605.  
  1606.         n = read_octal (octal_string);
  1607.  
  1608.         temp = savestring ("\\");
  1609.         if (n != -1)
  1610.           {
  1611.             string += 3;
  1612.             temp[0] = n;
  1613.           }
  1614.  
  1615.         c = 0;
  1616.         goto add_string;
  1617.           }
  1618.       
  1619.         case 't':
  1620.         case 'd':
  1621.  
  1622.           /* Make the current time/date into a string. */
  1623.           {
  1624.         long the_time = time (0);
  1625.         char *ttemp = ctime (&the_time);
  1626.         temp = savestring (ttemp);
  1627.  
  1628.         if (c == 't')
  1629.           {
  1630.             strcpy (temp, temp + 11);
  1631.             temp[8] = '\0';
  1632.           }
  1633.         else
  1634.           temp[10] = '\0';
  1635.  
  1636.         goto add_string;
  1637.           }
  1638.  
  1639.         case 'n':
  1640.           temp = savestring ("\r\n");
  1641.           goto add_string;
  1642.  
  1643.         case 's':
  1644.           {
  1645.         extern char *shell_name;
  1646.         temp = savestring (shell_name);
  1647.         goto add_string;
  1648.           }
  1649.     
  1650.         case 'w':
  1651.         case 'W':
  1652.           {
  1653.         /* Use the value of PWD because it is much more effecient. */
  1654. #define EFFICIENT
  1655. #ifdef EFFICIENT
  1656.         char *polite_directory_format (), t_string[MAXPATHLEN];
  1657.  
  1658.         temp = get_string_value ("PWD");
  1659.  
  1660.         if (!temp)
  1661.           getwd (t_string);
  1662.         else
  1663.           strcpy (t_string, temp);
  1664. #else
  1665.         getwd (t_string);
  1666. #endif  /* EFFICIENT */
  1667.  
  1668.         if (c == 'W')
  1669.           {
  1670.             char *rindex (), *dir = rindex (t_string, '/');
  1671.             if (dir)
  1672.               strcpy (t_string, dir + 1);
  1673.             temp = savestring (t_string);
  1674.           }
  1675.         else
  1676.           temp = savestring (polite_directory_format (t_string));
  1677.         goto add_string;
  1678.           }
  1679.       
  1680.         case 'u':
  1681.           {
  1682.         extern char *current_user_name;
  1683.         temp = savestring (current_user_name);
  1684.  
  1685.         goto add_string;
  1686.           }
  1687.  
  1688.         case 'h':
  1689.           {
  1690.         extern char *current_host_name;
  1691.         char *t_string, *index ();
  1692.  
  1693.         temp = savestring (current_host_name);
  1694.         if (t_string = index (temp, '.'))
  1695.           *t_string = '\0';
  1696.         
  1697.         goto add_string;
  1698.           }
  1699.  
  1700.         case '#':
  1701.           {
  1702.         extern int current_command_number;
  1703.         char number_buffer[20];
  1704.         sprintf (number_buffer, "%d", current_command_number);
  1705.         temp = savestring (number_buffer);
  1706.         goto add_string;
  1707.           }
  1708.  
  1709.         case '!':
  1710.           {
  1711.         extern int history_base, where_history ();
  1712.         char number_buffer[20];
  1713.  
  1714.         using_history ();
  1715.         if (get_string_value ("HISTSIZE"))
  1716.           sprintf (number_buffer, "%d",
  1717.                history_base + where_history ());
  1718.         else
  1719.           strcpy (number_buffer, "!");
  1720.         temp = savestring (number_buffer);
  1721.         goto add_string;
  1722.           }
  1723.  
  1724.         case '$':
  1725.           temp = savestring (geteuid () == 0 ? "#" : "$");
  1726.           goto add_string;
  1727.  
  1728.         case '\\':
  1729.           temp = savestring ("\\");
  1730.           goto add_string;
  1731.  
  1732.         default:
  1733.           temp = savestring ("\\ ");
  1734.           temp[1] = c;
  1735.  
  1736.         add_string:
  1737.           if (c)
  1738.         string++;
  1739.           result =
  1740.         (char *)sub_append_string (temp, result,
  1741.                        &result_index, &result_size);
  1742.           temp = (char *)NULL; /* Free ()'ed in sub_append_string (). */
  1743.           result[result_index] = '\0';
  1744.           break;
  1745.         }
  1746.     }
  1747.       else
  1748.     {
  1749.       while (3 + result_index > result_size)
  1750.         result = (char *)xrealloc (result, result_size += PROMPT_GROWTH);
  1751.  
  1752.       result[result_index++] = c;
  1753.       result[result_index] = '\0';
  1754.     }
  1755.     }
  1756.  
  1757.   /* I don't really think that this is a good idea.  Do you? */
  1758.   if (!find_variable ("NO_PROMPT_VARS"))
  1759.     {
  1760.       WORD_LIST *expand_string (), *list;
  1761.       char *string_list ();
  1762.  
  1763.       list = expand_string (result, 1);
  1764.       free (result);
  1765.       result = string_list (list);
  1766.       dispose_words (list);
  1767.     }
  1768.  
  1769.   return (result);
  1770. }
  1771.  
  1772. /* Report a syntax error, and restart the parser.  Call here for fatal
  1773.    errors. */
  1774. yyerror ()
  1775. {
  1776.   report_syntax_error ((char *)NULL);
  1777.   reset_parser ();
  1778. }
  1779.  
  1780. /* Report a syntax error with line numbers, etc.
  1781.    Call here for recoverable errors.  If you have a message to print,
  1782.    then place it in MESSAGE, otherwise pass NULL and this will figure
  1783.    out an appropriate message for you. */
  1784. report_syntax_error (message)
  1785.      char *message;
  1786. {
  1787.   if (message)
  1788.     {
  1789.       if (!interactive)
  1790.     {
  1791.       char *name = stream_name ? stream_name : "stdin";
  1792.       report_error ("%s:%d: `%s'", name, line_number, message);
  1793.     }
  1794.       else
  1795.     report_error ("%s", message);
  1796.  
  1797.       return;
  1798.     }
  1799.  
  1800.   if (shell_input_line && *shell_input_line)
  1801.     {
  1802.       char *error_token, *t = shell_input_line;
  1803.       register int i = shell_input_line_index;
  1804.       int token_end = 0;
  1805.  
  1806.       if (!t[i] && i)
  1807.     i--;
  1808.  
  1809.       while (i && t[i] == ' ' || t[i] == '\t' || t[i] == '\n')
  1810.     i--;
  1811.  
  1812.       if (i)
  1813.     token_end = i + 1;
  1814.  
  1815.       while (i && !member (t[i], " \n\t;|&"))
  1816.     i--;
  1817.  
  1818.       while (i != token_end && member (t[i], " \n\t"))
  1819.     i++;
  1820.  
  1821.       if (token_end)
  1822.     {
  1823.       error_token = (char *)alloca (1 + (token_end - i));
  1824.       strncpy (error_token, t + i, token_end - i);
  1825.       error_token[token_end - i] = '\0';
  1826.  
  1827.       report_error ("syntax error near `%s'", error_token);
  1828.     }
  1829.       else if ((i == 0) && (token_end == 0))    /* a 1-character token */
  1830.     {
  1831.       error_token = (char *) alloca (2);
  1832.       strncpy(error_token, t + i, 1);
  1833.       error_token[1] = '\0';
  1834.  
  1835.       report_error ("syntax error near `%s'", error_token);
  1836.     }
  1837.  
  1838.       if (!interactive)
  1839.     {
  1840.       char *temp = savestring (shell_input_line);
  1841.       char *name = stream_name ? stream_name : "stdin";
  1842.       int l = strlen (temp);
  1843.  
  1844.       while (l && temp[l - 1] == '\n')
  1845.         temp[--l] = '\0';
  1846.  
  1847.       report_error ("%s:%d: `%s'", name, line_number, temp);
  1848.       free (temp);
  1849.     }
  1850.     }
  1851.   else
  1852.     report_error ("Syntax error");
  1853. }
  1854.  
  1855. /* ??? Needed function. ??? We have to be able to discard the constructs
  1856.    created during parsing.  In the case of error, we want to return
  1857.    allocated objects to the memory pool.  In the case of no error, we want
  1858.    to throw away the information about where the allocated objects live.
  1859.    (dispose_command () will actually free the command. */
  1860. discard_parser_constructs (error_p)
  1861.      int error_p;
  1862. {
  1863. /*   if (error_p) {
  1864.      fprintf (stderr, "*");
  1865.   } */
  1866. }
  1867.    
  1868. /* Do that silly `type "bye" to exit' stuff.  You know, "ignoreeof". */
  1869.  
  1870. /* The number of times that we have encountered an EOF character without
  1871.    another character intervening.  When this gets above the limit, the
  1872.    shell terminates. */
  1873. int eof_encountered = 0;
  1874.  
  1875. /* The limit for eof_encountered. */
  1876. int eof_encountered_limit = 10;
  1877.  
  1878. /* If we have EOF as the only input unit, this user wants to leave
  1879.    the shell.  If the shell is not interactive, then just leave.
  1880.    Otherwise, if ignoreeof is set, and we haven't done this the
  1881.    required number of times in a row, print a message. */
  1882. handle_eof_input_unit ()
  1883. {
  1884.   extern int login_shell, EOF_Reached;
  1885.  
  1886.   if (interactive)
  1887.     {
  1888.       /* If the user wants to "ignore" eof, then let her do so, kind of. */
  1889.       if (find_variable ("ignoreeof") || find_variable ("IGNOREEOF"))
  1890.     {
  1891.       if (eof_encountered < eof_encountered_limit)
  1892.         {
  1893.           fprintf (stderr, "Use \"%s\" to leave the shell.\n",
  1894.                login_shell ? "logout" : "exit");
  1895.           eof_encountered++;
  1896.           return;
  1897.         } 
  1898.     }
  1899.  
  1900.       /* In this case EOF should exit the shell.  Do it now. */
  1901.       reset_parser ();
  1902.       exit_builtin ((WORD_LIST *)NULL);
  1903.     }
  1904.   else
  1905.     {
  1906.       /* We don't write history files, etc., for non-interactive shells. */
  1907.       EOF_Reached = 1;
  1908.     }
  1909. }
  1910.